package 复习集合;

import java.util.*;

public class Text1 {
    static final int Enter=1;
    static final int Exit=0;
    public static void main(String[] args) {
        List<Student> list=new ArrayList<>();
        list.add(new Student("张三",18));
        list.add(new Student("张李四",17));
        while (true){
            System.out.print("请输入0/1进行操作:");
            int next = new Scanner(System.in).nextInt();
            if (next==Enter){
                System.out.print("请你输入学生姓名:");
                String next1 = new Scanner(System.in).next();
                System.out.print("请你输入学生年纪");
                int i = new Scanner(System.in).nextInt();

                if (Determine_Name(list,next1)){
                    System.out.println("录入失败!");
                }else {

                    list.add(new Student(next1,i));
                }

            }
            if (next==Exit){
                Iterator<Student> iterator = list.iterator();
                while (iterator.hasNext()){
                    System.out.println(iterator.next());

                }
            }if (next!=1&&next!=0){
                System.out.println("输入错误!");
            }

        }
    }

    /**
     * 判断是否重复
     * @param list
     * 集合
     * @param name
     * 判断元素
     * @return//返回true就是处在,返回false就是不存在
     */
    public static boolean Determine_Name(List<Student> list,String name){
        for (Student student : list) {
            if (student.getName().equals(name)){
                return true;
            }
        }
        return false;
    }
}

/**
 * 学生类,包含姓名,年纪
 */
class Student{
    private  String name;
    private  int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    /**
     * 获取名字
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置名字
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取年龄
     * @return age
     */
    public int getAge() {
        return age;
    }

    /**
     * 设置年龄
     * @param age
     */
    public void setAge(int age) {
        this.age = age;
    }

    public String toString() {
        return "Student{name = " + name + ", age = " + age + "}";
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age && Objects.equals(name, student.name);
    }
}
